#!/bin/bash

source /home/shell/util.sh

firewall-must-run

apt install -y sshpass

my_host=$host_addr

read -p "本机root账号密码[123456]：" my_host_passwd
my_host_passwd=${my_host_passwd:-"123456"}

read -p "远端服务器root账号密码[123456]：" remote_host_passwd
remote_host_passwd=${remote_host_passwd:-"123456"};

read -p "设置mysql master的repl账号密码[123456]：" master_mysql_passwd
master_mysql_passwd=${master_mysql_passwd:-"123456"};

serversPrint
read -p "请输入Master地址：" remote_host
if [[ -z "${remote_host}" ]]; then
    printFail "请输入Master地址，程序退出，请重新执行。"
    exit;
fi


printInfo "准备本地和远端服务器SSH环境";
# 本地环境
if [ ! -d ~/.ssh/ ]; then
    mkdir ~/.ssh/&&chmod 700 ~/.ssh/
fi
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
touch ~/.ssh/known_hosts
ssh-keyscan $remote_host >> ~/.ssh/known_hosts
# 远端环境
sshpass -p $remote_host_passwd ssh -o "StrictHostKeyChecking no" root@$remote_host bash<<EOF
if [ ! -d ~/.ssh/ ]; then
    mkdir ~/.ssh/&&chmod 700 ~/.ssh/
fi
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
touch ~/.ssh/known_hosts
ssh-keyscan $my_host >> ~/.ssh/known_hosts
EOF

printInfo "远端服务器，生成证书并发送到本地服务器root@$my_host"
sshpass -p $remote_host_passwd ssh -o "StrictHostKeyChecking no" root@$remote_host bash<<EOF
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
sshpass -p $my_host_passwd ssh-copy-id -i ~/.ssh/id_rsa.pub root@$my_host
EOF

printInfo "本地服务器，生成证书并发送到远端服务器root@$remote_host"
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""
sshpass -p $remote_host_passwd ssh-copy-id -i ~/.ssh/id_rsa.pub root@$remote_host

# 安装mysql
bash /home/shell/ubuntu/mysql-install.sh

# 修改MySQL主从配置
if [ $(ostype) = "Ubuntu" ];
then
    printInfo "修改Ubuntu MySQL配置";
    sed -i "s/server-id=1/server-id=2/g" /etc/mysql/mysql.conf.d/mysqld.cnf
    sed -i "/server-id/a\relay-log=mysql-relay-bin\nlog-slave-updates=1\nread-only=1" /etc/mysql/mysql.conf.d/mysqld.cnf
    # 让配置生效
    systemctl restart mysql
else
    printInfo "修改CentOS MySQL配置";
    sed -i "s/server-id=1/server-id=2/g" /etc/my.cnf
    sed -i "/server-id/a\relay-log=mysql-relay-bin\nlog-slave-updates=1\nread-only=1" /etc/my.cnf
    # 让配置生效
    systemctl restart mysqld
fi


printInfo "master新建同步用户rep1";
ssh root@$remote_host<<EOF
    mysql --login-path=local --connect-expired-password -e "DROP USER if EXISTS repl@$my_host;CREATE USER repl@$my_host IDENTIFIED WITH mysql_native_password BY '$master_mysql_passwd';GRANT ALL PRIVILEGES ON *.* TO repl@$my_host;FLUSH PRIVILEGES;"
EOF

# 设置从库用户
printInfo "设置从库用户";
mysql --login-path=local<<EOF
    use mysql;
    update user set host='localhost' where user='root';
    FLUSH PRIVILEGES;
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';
    FLUSH PRIVILEGES;
EOF
mysql --login-path=local --connect-expired-password -e "drop user if exists 'dev_user'@'%';";

sleep 5s

# 创建主从同步
printInfo "Slave设置主从同步";
mysql --login-path=local --connect-expired-password -e "change master to master_host='$remote_host', master_user='repl', master_password='$master_mysql_passwd', master_log_file='mysql-bin.000001', master_log_pos=0,master_connect_retry=30,get_master_public_key=1;"
printInfo "开始同步";
mysql --login-path=local --connect-expired-password -e "RESET SLAVE;"
mysql --login-path=local --connect-expired-password -e "START SLAVE;"
printInfo "同步状态";
mysql --login-path=local --connect-expired-password -e "SHOW SLAVE STATUS\G;"